var pomelo = require('pomelo');
var async = require('async');
var wt = require('async').waterfall;

var utils = require('../util/utils');

var log = console.log;

module.exports = function (app, opts) {
    return new resetRedisData(app, opts);
};

var DEFAULT_INTERVAL = 3000;

var resetRedisData = function (app, opts) {
    this.app = app;
    this.timerId = null;
};

resetRedisData.name = '__resetRedisData__';

resetRedisData.prototype.start = function (cb) {
    console.log('resetRedisData Start');

    var client = pomelo.app.get('redisUser');
    var gameId = 1;  // 当前游戏是百家乐

    var self = this;
    var onlineGameUser = 'onlineGameUser';
    var singleServer = 1;  // 是不是每一个服务只放一个游戏
    wt([
        function (cb) {
            // 读取所有在线用户的信息
            client.hgetall(onlineGameUser, function (err, rel) {
                if (err) {
                    return cb(err);
                }
                var keys = {}, key;
                for (key in rel) {
                    // if (singleServer && parseInt(rel[key]) === gameId) {
                    //     keys[key] = 0;
                    // }
                    // 重置所有用户登录信息
                    keys[key] = 0;
                }
                console.log('更新前在线用户数据为：', rel);
                cb(null, keys);
            });
        },
        function (keys, cb) {
            // 更新在线用户数据
            var count = 0, key;
            for (key in keys) {
                count++;
            }
            if (count === 0) {
                // 如果没有在线用户，直接跳过设置
                return cb(null);
            }
            // 将当前游戏在线用户的游戏 id 置为 0
            console.log('更新相关用户数据为：', keys);
            client.hmset(onlineGameUser, keys, function (err, rel) {
                if (err) {
                    return cb(err);
                }
                cb(null);
            });
        },
        function (cb) {
            // 再次读取所有在线用户的信息
            client.hgetall(onlineGameUser, function (err, rel) {
                if (err) {
                    return cb(err);
                }
                console.log('更新后在线用户数据为：', rel);
                cb(null);
            });
        }
    ],
        function (err, rel) {
            if (err) {
                throw err;
            }
            process.nextTick(cb);
            console.log('已经重置了redis 相关数据！');
        });
    // mysql.query('select * from game_info where id = 1', [], function (err, rows) {
    //     if (err) throw err;
    //     console.log(rows.length);
    //     console.log(rows[0]);
    //     process.nextTick(cb);
    // });
    //   this.timerId = setInterval(function() {
    //     console.log(self.app.getServerId() + ": resetRedisData!");
    //     }, this.interval);
    //   process.nextTick(cb);
};

resetRedisData.prototype.afterStart = function (cb) {
    console.log('resetRedisData afterStart');
    // clearInterval(this.timerId);
    process.nextTick(cb);
    // log(this.app.get('sessionService'));
};

resetRedisData.prototype.stop = function (force, cb) {
    console.log('resetRedisData stop');
    process.nextTick(cb);
};
